package com.vcit.zhaosheng.controller;

import com.vcit.common.annotation.Log;
import com.vcit.common.core.controller.BaseController;
import com.vcit.common.core.domain.AjaxResult;
import com.vcit.common.core.page.TableDataInfo;
import com.vcit.common.enums.BusinessType;
import com.vcit.common.utils.DateUtils;
import com.vcit.common.utils.poi.ExcelUtil;
import com.vcit.system.domain.student.StudentDTO;
import com.vcit.system.domain.vo.EnrollStudentVo;
import com.vcit.system.service.enroll.EnrollService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Api("招生信息管理")
@RestController
@RequestMapping("/system/enroll")
public class ZhaoController  extends BaseController {

    @Autowired
    private EnrollService enrollService;

    /**
     * 查询学生招生列表
     */
    @ApiOperation("获取招生学生信息列表")
    @PreAuthorize("@ss.hasPermi('system:enroll:lsit')")
    @GetMapping("/list")
    public TableDataInfo list(EnrollStudentVo enrollStudentVo)
    {
        startPage();
        List<EnrollStudentVo> list = enrollService.selectZhaoshengStudentList(enrollStudentVo);
        return getDataTable(list);
    }



    /**
     * 获取学生招生详细信息
     */
    @ApiOperation("获取招生学生信息详情")
    @PreAuthorize("@ss.hasPermi('system:enroll:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(enrollService.selectZhaoshengStudentById(id));
    }

    /**
     * 新增学生招生
     */
    @ApiOperation("新增招生学生信息")
    @PreAuthorize("@ss.hasPermi('system:enroll:add')")
    @Log(title = "学生招生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EnrollStudentVo enrollStudentVo)
    {
        enrollStudentVo.setCreateTime(DateUtils.getNowDate());
        enrollStudentVo.setCreateBy(getUsername());
        return toAjax(enrollService.insertZhaoshengStudent(enrollStudentVo));
    }

    /**
     * 修改学生招生
     */
    @ApiOperation("修改招生学生信息")
    @PreAuthorize("@ss.hasPermi('system:enroll:edit')")
    @Log(title = "学生招生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EnrollStudentVo enrollStudentVo)
    {
        enrollStudentVo.setUpdateTime(DateUtils.getNowDate());
        enrollStudentVo.setUpdateBy(getUsername());
        return toAjax(enrollService.updateZhaoshengStudent(enrollStudentVo));
    }
    
    /**
     * 学生分配班级
     */
    @ApiOperation("学生分配班级")
    @PreAuthorize("@ss.hasPermi('system:enroll:edit')")
    @Log(title = "学生招生", businessType = BusinessType.UPDATE)
    @PutMapping("/assignClazz")
    public AjaxResult assignClazz(@RequestBody StudentDTO studentDTO){
        studentDTO.setUpdateTime(DateUtils.getNowDate());
        studentDTO.setUpdateBy(getUsername());
        enrollService.assignClassToStudent(studentDTO);
        return AjaxResult.success("分班成功");

    }

    /**
     * 删除学生招生
     */
    @ApiOperation("删除招生学生信息")
    @PreAuthorize("@ss.hasPermi('system:enroll:remove')")
    @Log(title = "学生招生", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(enrollService.deleteZhaoshengStudentByIds(ids));
    }

    /**
     * 导出学生招生列表
     */
    @ApiOperation("导出招生学生信息")
    @PreAuthorize("@ss.hasPermi('system:enroll:export')")
    @Log(title = "学生招生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EnrollStudentVo enrollStudentVo) {
        try {
            List<EnrollStudentVo> list = enrollService.selectZhaoshengStudentList(enrollStudentVo);
            ExcelUtil<EnrollStudentVo> util = new ExcelUtil<>(EnrollStudentVo.class);
            util.exportExcel(response, list, "招生学生数据");
        } catch (Exception e) {
            logger.error("导出招生学生数据失败", e);
            throw new RuntimeException("导出招生学生数据失败，请联系管理员");
        }
    }

    /**
     * 导入学生招生数据
     */
    @ApiOperation("导入招生学生信息")
    @PreAuthorize("@ss.hasPermi('system:enroll:import')")
    @Log(title = "学生招生", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file,boolean updateSupport) {
        if (file == null) {
            return error("请选择要导入的文件");
        }
        try {
            ExcelUtil<EnrollStudentVo> util = new ExcelUtil<>(EnrollStudentVo.class);
            List<EnrollStudentVo> studentList = util.importExcel(file.getInputStream());
            if (studentList == null || studentList.isEmpty()) {
                return error("导入数据为空");
            }
            String operName = getUsername();
            String message = enrollService.importZhaoshengStudent(studentList,operName,updateSupport);
            return success(message);
        } catch (Exception e) {
            logger.error("导入招生学生数据失败", e);
            return error("导入招生学生数据失败：" + e.getMessage());
        }
    }

    /**
     * 下载导入模板
     */
    @ApiOperation("下载导入模板")
    @PreAuthorize("@ss.hasPermi('system:enroll:import')")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        try {
            ExcelUtil<EnrollStudentVo> util = new ExcelUtil<>(EnrollStudentVo.class);
            util.importTemplateExcel(response, "招生学生数据");
        } catch (Exception e) {
            logger.error("下载导入模板失败", e);
            throw new RuntimeException("下载导入模板失败，请联系管理员");
        }
    }
}
